package com.example.administrator.xiaoxiang.activity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.example.administrator.xiaoxiang.R;
import com.example.administrator.xiaoxiang.adapter.HomeGoodsAdapter;
import com.example.administrator.xiaoxiang.bean.HomeGoodGroupItem;
import com.example.administrator.xiaoxiang.bean.HomeSecondJsonBean;
import com.example.administrator.xiaoxiang.entity.ShopingcartEntity;
import com.example.administrator.xiaoxiang.gen.DaoMaster;
import com.example.administrator.xiaoxiang.gen.DaoSession;
import com.example.administrator.xiaoxiang.gen.ShopingcartEntityDao;
import com.example.administrator.xiaoxiang.url.URLResource;
import com.example.administrator.xiaoxiang.utils.AsyncTaskHelper;
import com.example.administrator.xiaoxiang.utils.ImageDownloadHelper;
import com.example.administrator.xiaoxiang.utils.RecyclerViewDivider;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * 二级跳转的页面
 */

public class HomeSecondActivity extends AppCompatActivity
        implements AsyncTaskHelper.OnDataDownloadListener,ImageDownloadHelper.OnImageDownloadListener {
    private static final String TAG = "HomeSecondActivity";
    private Intent intent;
    private String secondUrl;
    private AsyncTaskHelper helper;
    private List<HomeGoodGroupItem> data;
    private RecyclerView recyclerView;
    private HomeGoodsAdapter adapter;
    private RecyclerViewHeader header;
    private ImageView imageViewHeader;
    private int count = 0;
    private TextView count_textView;
    private ImageDownloadHelper imageDownloadHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_second);
        init();
        loadData();
    }

    private void loadData() {
        helper.downloadData(secondUrl, this);
    }

    private void init() {
        intent = getIntent();
        secondUrl = URLResource.HomeSecondBaseUrl + intent.getStringExtra("secondUrl");
//        Toast.makeText(this, secondUrl, Toast.LENGTH_SHORT).show();
        helper = new AsyncTaskHelper();
        imageDownloadHelper = new ImageDownloadHelper();
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView_home_second);
        count_textView = (TextView) findViewById(R.id.circle);
        data = new ArrayList<>();
        adapter = new HomeGoodsAdapter(this, data, true);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(gridLayoutManager);
        header = RecyclerViewHeader.fromXml(this, R.layout.header_recycler_view);
        recyclerView.addItemDecoration(new RecyclerViewDivider(this,GridLayoutManager.VERTICAL,10, Color.parseColor("#dcd7d7")));
        recyclerView.addItemDecoration(new RecyclerViewDivider(this,GridLayoutManager.HORIZONTAL,10,Color.parseColor("#dcd7d7")));

        imageViewHeader = (ImageView) header.findViewById(R.id.header);
        header.attachTo(recyclerView);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new HomeGoodsAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(String url) {
                Intent intent = new Intent(HomeSecondActivity.this,DetailPagesActivity.class);
                intent.putExtra("getGoodsCode",url);
//                Toast.makeText(HomeSecondActivity.this, url, Toast.LENGTH_SHORT).show();
                startActivity(intent);
            }
        });
    }

    @Override
    public void onDataDownload(byte[] result) {
        String json = new String(result);
//        Log.e("dd", "onDataDownload: " + json);
        Gson gson = new Gson();
        HomeSecondJsonBean homeSecondJsonBean = gson.fromJson(json, HomeSecondJsonBean.class);
//        Glide.with(this).load(homeSecondJsonBean.getBody().getData().getBannerUrl()).placeholder(R.drawable.home_banner_bitmap).into(imageViewHeader);
        imageViewHeader.setTag(homeSecondJsonBean.getBody().getData().getBannerUrl());
        imageDownloadHelper.myDownloadImage(this,homeSecondJsonBean.getBody().getData().getBannerUrl(),imageViewHeader,this);
        //精品分组
        for (int i = 0; i < homeSecondJsonBean.getBody().getData().getGoods().size(); i++) {
            HomeGoodGroupItem homeGoodGroupItem = new HomeGoodGroupItem();
            homeGoodGroupItem.subTitle = homeSecondJsonBean.getBody().getData().getGoods().get(i).getGoodsName();
//            Log.e("ddd", "onDataDownload: " + homeGoodGroupItem.subTitle);
            homeGoodGroupItem.peroidInstalmentAmount = homeSecondJsonBean.getBody().getData().getGoods().get(i).getPeroidInstalmentAmount();
            homeGoodGroupItem.instalmentPeriods = homeSecondJsonBean.getBody().getData().getGoods().get(i).getInstalmentPeriods() + "";
            homeGoodGroupItem.directPaymentAmount = homeSecondJsonBean.getBody().getData().getGoods().get(i).getDirectPaymentAmount();
            homeGoodGroupItem.thumbnailUrl = homeSecondJsonBean.getBody().getData().getGoods().get(i).getThumbnailUrl();
            homeGoodGroupItem.goodsCode = homeSecondJsonBean.getBody().getData().getGoods().get(i).getGoodsCode();
            data.add(homeGoodGroupItem);
        }
        adapter.notifyDataSetChanged();
    }

    public void click(View view) {
        switch (view.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.shop_cart:
                Intent intent = new Intent(this,ShopCardActivity.class);
                startActivity(intent);
                break;
        }
    }
    private void initDb() {
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(getApplicationContext(), "XX.db", null);
        SQLiteDatabase db = openHelper.getReadableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();
        ShopingcartEntityDao shopingcartEntityDao = daoSession.getShopingcartEntityDao();
        List<ShopingcartEntity> list =
                shopingcartEntityDao.queryBuilder().build().list();
        for (ShopingcartEntity shopingcartEntity : list) {
            count+=Integer.parseInt(shopingcartEntity.getNum());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        count = 0;
        initDb();
        if (count==0) {
            count_textView.setVisibility(View.GONE);
        }else{
            count_textView.setVisibility(View.VISIBLE);
            count_textView.setText(count+"");
        }
    }


    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onImageDownload(Bitmap bitmap, String imgUrl) {

    }

    @Override
    public void onImageDownload(Bitmap bitmap, String imgUrl, ImageView imageView) {
        imageView.setImageBitmap(bitmap);
    }
}
